<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="供应商编号" prop="supplierCode">
        <el-input v-model="queryParams.supplierCode" placeholder="请输入供应商编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属行业" prop="industryId">
        <el-select v-model="queryParams.industryId" placeholder="请选择所属行业" clearable>
          <el-option v-for="dict in dict.type.py_industry" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="进货负责人" prop="purchaseResponsibleId">
        <el-input v-model="queryParams.purchaseResponsibleId" placeholder="请输入进货负责人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.py_suppliers_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['unit:suppliers:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['unit:suppliers:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['unit:suppliers:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['unit:suppliers:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- {{ form }} -->
    <el-table v-loading="loading" :data="suppliersList" @selection-change="handleSelectionChange">
      <el-table-column  type="selection" width="55" align="center" />
      <el-table-column fixed label="供应商ID" align="center" prop="supplierId" width="180px"/>
      <el-table-column fixed label="供应商编号" align="center" prop="supplierCode" width=180px>
      <template slot-scope="scope">
          <router-link
            :to="`/wms/unit/suppliers/query/${scope.row.supplierId}`"
            tag="span"
            style="color: blue; cursor: pointer;"
          >
            {{ scope.row.supplierCode }}
          </router-link>
        </template>
      </el-table-column>
       <el-table-column fixed label="供应商名称" align="center" prop="supplierName" width="180px"/>
      <el-table-column fixed label="所属行业" align="center" prop="industryId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.py_industry" :value="scope.row.industryId" />
        </template>
      </el-table-column>
      <el-table-column  label="供应商分类" align="center" prop="supplierCategoryId">
        <template slot-scope="scope">
          <el-span v-if="scope.row.supplierCategoryId==1">成品供应商</el-span>
          <el-span v-if="scope.row.supplierCategoryId==2">零件供应商</el-span>
          <el-span v-if="scope.row.supplierCategoryId==3">数码供应商</el-span>
        </template>
      </el-table-column>
      <el-table-column  label="联系人" align="center" prop="contactPerson" />
      <el-table-column  label="联系电话" align="center" prop="contactPhone" width="180px"/>
      <el-table-column  label="电子邮箱" align="center" prop="email" width="180px"/>
      <el-table-column  label="进货部门" align="center" prop="deptName" />
      <el-table-column  label="进货负责人" align="center" prop="purchaseResponsibleId" width="180px"/>
      <el-table-column   label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.py_suppliers_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="供应商地址" align="center" prop="supplierAddress" width="180px"/>
      <el-table-column label="备注" align="center" prop="remarks" width="180px"/>
      <el-table-column label="账户名称" align="center" prop="pyAccountList[0].accountName" width="180px" />
      <el-table-column label="纳税人识别号" align="center" prop="pyAccountList[0].taxId" width="180px"/>
      <el-table-column label="银行账户" align="center" prop="pyAccountList[0].bankAccount" width="180px"/>
      <el-table-column label="开户地址" align="center" prop="pyAccountList[0].accountOpeningAddress" width="180px" />
      <el-table-column label="开户行地址" align="center" prop="pyAccountList[0].accountAddress" width=180px />
      <el-table-column label="操作人" align="center" prop="manager" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['unit:suppliers:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['unit:suppliers:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加或修改供应商信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"  label-width="100px">
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="供应商编号" prop="supplierCode" >
          <el-input v-model="form.supplierCode" placeholder="请输入供应商编号" readonly />
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="所属行业" prop="industryId">
          <el-select v-model="form.industryId" placeholder="请选择所属行业">
            <el-option v-for="dict in dict.type.py_industry" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="供应商分类" prop="supplierCategoryId">
          <el-select v-model="form.supplierCategoryId" placeholder="请选择供应商分类">
            <el-option
              v-for="dict in dict.type.py_suppliers"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="11">
         <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="进货部门" prop="purchaseDepartmentId">
          <treeselect v-model="form.purchaseDepartmentId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="进货负责人" prop="purchaseResponsibleId">
          <el-input v-model="form.purchaseResponsibleId" placeholder="请输入进货负责人" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option v-for="dict in dict.type.py_suppliers_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="供应商地址" prop="supplierAddress">
          <el-input v-model="form.supplierAddress" placeholder="请输入供应商地址" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">财务信息信息</el-divider>
        <br/>
        <el-row :gutter="20">
          <el-col :span="11">
          <el-form-item label="账户名称" prop="pyAccount">
          <el-input v-model="form.pyAccount.accountName" placeholder="请输入账户名称" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="开户地址" prop="pyAccountList[0].accountOpeningAddress">
              <el-input v-model="form.pyAccount.accountOpeningAddress" placeholder="请输入开户地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
         <el-form-item label="纳税人识别号" prop="pyAccountList[0].taxId">
          <el-input v-model="form.pyAccount.taxId" placeholder="请输入纳税人识别号" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
         <el-form-item label="银行账户" prop="pyAccountList[0].accountAddress">
          <el-input v-model="form.pyAccount.accountAddress" placeholder="请输入银行账户" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
         <el-form-item label="开户银行" prop="pyAccountList[0].bankAccount">
          <el-input v-model="form.pyAccount.bankAccount" placeholder="请输入开户银行" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="来往单位" prop="pyAccountList[0].sourceUnit" hidden="hidden">
              <el-input v-model="form.pyAccount.sourceUnit" readonly />
            </el-form-item>

          </el-col>
        </el-row>
        <!-- <el-form-item label="创建时间" prop="createdTime">
          <el-input v-model="form.pyAccount.createdTime" />
        </el-form-item> -->
        <!-- <el-form-item label="伪删除" prop="isDeleted">
          <el-input v-model="form.pyAccount.isDeleted" placeholder="请输入伪删除" />
        </el-form-item> -->
      <!-- <el-divider content-position="center">财务信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPyAccount">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePyAccount">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="pyAccountList" :row-class-name="rowPyAccountIndex"
          @selection-change="handlePyAccountSelectionChange" ref="pyAccount">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="账户名称" prop="accountName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.accountName" placeholder="请输入账户名称" />
            </template>
          </el-table-column>
          <el-table-column label="开户行地址" prop="accountAddress" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.accountAddress" placeholder="请输入开户行地址" />
            </template>
          </el-table-column>
          <el-table-column label="纳税人识别号" prop="taxId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.taxId" placeholder="请输入纳税人识别号" />
            </template>
          </el-table-column>
          <el-table-column label="银行账户" prop="bankAccount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bankAccount" placeholder="请输入银行账户" />
            </template>
          </el-table-column>
          <el-table-column label="开户地址" prop="accountOpeningAddress" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.accountOpeningAddress" placeholder="请输入开户地址" />
            </template>
          </el-table-column>
          <el-table-column label="逻辑删除" prop="isDelete" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isDelete" placeholder="请输入逻辑删除" />
            </template>
          </el-table-column>
        </el-table> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listSuppliers, getSuppliers, delSuppliers, addSuppliers, updateSuppliers, getCode } from "@/api/unit/suppliers";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Suppliers",
  dicts: ['py_industry', 'py_suppliers_status','py_suppliers'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPyAccount: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商信息表格数据
      suppliersList: [],
      // 财务信息表格数据
      pyAccountList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierCode: null,
        industryId: null,
        purchaseResponsibleId: null,
        status: null,
      },
      // 表单参数
      form: {
        supplierId: null,
        supplierCode: null,
        industryId: null,
        supplierCategoryId: null,
        contactPerson: null,
        supplierName: null,
        contactPhone: null,
        email: null,
        purchaseDepartmentId: null,
        purchaseResponsibleId: null,
        status: null,
        supplierAddress: null,
        remarks: null,
        manager: null,
        createdAt: null,
        updatedAt: null,
        isDeleted: null,

        pyAccount: {
        accountName: null,
        taxId: null,
        accountAddress: null,
        bankAccount: null,
        accountOpeningAddress: null,
        sourceUnit: null
      },
        number:undefined,
        codeId:undefined,
      },
      // 表单校验
      rules: {
        createdAt: [
          { required: true, message: '创建时间不能为空', trigger: "blur" }
        ],
        updatedAt: [
          { required: true, message: '更新时间不能为空', trigger: "blur" }
        ],
        supplierName: [
          { required: true, message: '供应商名称不能为空', trigger: "blur"}
        ],
        email: [
          { required: true, message: '电子邮箱不能为空', trigger: "blur" }
        ],
        purchaseResponsibleId: [
          { required: true, message: '进货负责人不能为空', trigger: "blur" }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: "blur"}
        ]
      },
      supplierIds:[],
      deptOptions: undefined,
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询供应商信息列表 */
    getList() {
      this.loading = true;
      listSuppliers(this.queryParams).then(response => {
        // console.log("萨达安安:"+JSON.stringify( response.rows, null, 2));
        this.suppliersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplierId: null,
        supplierCode: null,
        industryId: null,
        supplierCategoryId: null,
        contactPerson: null,
        supplierName: null,
        contactPhone: null,
        email: null,
        purchaseDepartmentId: null,
        purchaseResponsibleId: null,
        status: null,
        supplierAddress: null,
        remarks: null,
        manager: null,
        createdAt: null,
        updatedAt: null,
        isDeleted: '0',
        pyAccount: {
        accountName: null,
        taxId: null,
        accountAddress: null,
        bankAccount: null,
        accountOpeningAddress: null,
        sourceUnit: null
         }
      };
      this.pyAccountList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.supplierId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商信息";
        /** 获取编号*/
        getCode(11).then(response => {
        console.log(response);
        this.form.supplierCode=response.sequence;
        this.form.number=response.number;
        this.form.codeId=response.codeId;
    });
    },
    /** 修改按钮操作 */
      handleUpdate(row) {
      this.reset();
      const supplierId = row.supplierId || this.ids
      getSuppliers(supplierId).then(response => {
          for(let yi in this.suppliersList) {
            if(this.suppliersList[yi].pyAccountList[0].sourceUnit === response.data.supplierCode) {
              // alert( this.suppliersList[yi].pyAccountList[0].accountId);
               this.form.supplierId = this.suppliersList[yi].supplierId;
               this.form.supplierName = this.suppliersList[yi].supplierName;
               this.form.supplierCode = this.suppliersList[yi].supplierCode;
               this.form.industryId = this.suppliersList[yi].industryId;
               this.form.supplierCategoryId = this.suppliersList[yi].supplierCategoryId;
               this.form.contactPerson = this.suppliersList[yi].contactPerson;
               this.form.email = this.suppliersList[yi].email;
               this.form.purchaseDepartmentId = this.suppliersList[yi].purchaseDepartmentId;
               this.form.purchaseResponsibleId = this.suppliersList[yi].purchaseResponsibleId;
               this.form.supplierAddress = this.suppliersList[yi].supplierAddress;
               this.form.remarks = this.suppliersList[yi].remarks;
               this.form.status = this.suppliersList[yi].status;
               this.form.contactPhone = this.suppliersList[yi].contactPhone;
               this.form.pyAccount.sourceUnit = this.suppliersList[yi].pyAccountList[0].sourceUnit;
               this.form.pyAccount.accountId = this.suppliersList[yi].pyAccountList[0].accountId;
               this.form.pyAccount.accountName = this.suppliersList[yi].pyAccountList[0].accountName;
               this.form.pyAccount.remarks = this.suppliersList[yi].pyAccountList[0].remarks;
               this.form.pyAccount.taxId = this.suppliersList[yi].pyAccountList[0].taxId;
               this.form.pyAccount.accountOpeningAddress = this.suppliersList[yi].pyAccountList[0].accountOpeningAddress;
               this.form.pyAccount.sourceUnit = this.suppliersList[yi].pyAccountList[0].sourceUnit;
               this.form.pyAccount.bankAccount = this.suppliersList[yi].pyAccountList[0].bankAccount;
               this.form.pyAccount.accountAddress = this.suppliersList[yi].pyAccountList[0].accountAddress;
          }
        }
        this.open = true;
        this.title = '修改供应商信息';
           });
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.supplierId != null) {
            this.form.pyAccount.sourceUnit = this.form.supplierCode;
            updateSuppliers(this.form).then(response => {
              this.getList();
              this.$modal.msgSuccess("修改成功");
              this.open = false;
            });
          } else {
            this.form.pyAccount.sourceUnit = this.form.supplierCode;
            addSuppliers(this.form).then(response => {
              this.getList();
              this.$modal.msgSuccess("新增成功");
              this.open = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supplierIds = row.supplierId || this.ids;
      this.$modal.confirm('是否确认删除供应商信息编号为"' + supplierIds + '"的数据项？').then(function() {
        return delSuppliers(supplierIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('删除成功');
      }).catch(() => {
        alert(1234)
      });
    },
	/** 财务信息序号 */
    rowPyAccountIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 财务信息添加按钮操作 */
    handleAddPyAccount() {
      let obj = {};
      obj.accountName = "";
      obj.accountAddress = "";
      obj.taxId = "";
      obj.bankAccount = "";
      obj.accountOpeningAddress = "";
      obj.isDelete = "";
      this.pyAccountList.push(obj);
    },
    /** 财务信息删除按钮操作 */
    handleDeletePyAccount() {
      if (this.checkedPyAccount.length == 0) {
        this.$modal.msgError("请先选择要删除的财务信息数据");
      } else {
        const pyAccountList = this.pyAccountList;
        const checkedPyAccount = this.checkedPyAccount;
        this.pyAccountList = pyAccountList.filter(function(item) {
          return checkedPyAccount.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePyAccountSelectionChange(selection) {
      this.checkedPyAccount = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('unit/suppliers/export', {
        ...this.queryParams
      }, `suppliers_${new Date().getTime()}.xlsx`)
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
  }
};
</script>
